﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;

namespace KasaTakip
{
    public static class Methods
    {
        public static string GetDescription(this Enum currentEnum)
        {
            string description = string.Empty;

            DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(currentEnum.GetType().GetField(currentEnum.ToString()), typeof(DescriptionAttribute));

            if (da != null)
            {
                description = da.Description;
            }

            else
            {
                description = currentEnum.ToString();
            }

            return description;
        }
    }

    public enum ReportTypeOption : byte
    {
        [Description("Kasiyer Raporu")]
        KasiyerRaporu = 0,

        [Description("Tüm Kasiyerler Özet Raporu")]
        KasiyerOzetRaporu = 1,

        [Description("Mağaza Föyü")]
        AnaKasaRaporu = 2
    }

    public class Printing
    {
        public int Id { get; set; }

        /// <summary>Rapor Tipi</summary>
        public ReportTypeOption ReportType { get; set; }
        /// <summary>İçerik</summary>
        public byte[] Content { get; set; }

        // Methods
        public static string GetPath()
        {
            string path = string.Format("{0}\\Dizaynlar", System.Windows.Forms.Application.StartupPath);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            return path;
        }

        public static string GetPath(ReportTypeOption reportType)
        {
            return string.Format("{0}\\{1}.repx", GetPath(), reportType);
        }

        public static FastReport.Report GetReport(ReportTypeOption reportType)
        {
            FastReport.Report report = FastReport.Report.FromFile(GetPath(reportType));

            if (report == null)
            {
                throw new NullReferenceException("Rapor şablonu yüklenemedi!");
            }

            return report;
        }


        public static void CreateReportTemplates()
        {
            using (KasaEntity ent = new KasaEntity())
            {
                foreach (ReportTypeOption reportType in Enum.GetValues(typeof(ReportTypeOption)))
                {
                    BelgeSablonu belge = (from b in ent.BelgeSablonlari where b.ReportType == (byte)reportType select b).FirstOrDefault();
                    if (belge == null)
                    {
                        new FastReport.Report().Save(Printing.GetPath(reportType));
                        ent.AddToBelgeSablonlari(new BelgeSablonu()
                        {
                            ReportType = (byte)reportType,
                            TemplateContent = File.ReadAllBytes(Printing.GetPath(reportType))
                        });
                    }
                    else
                    {
                        File.WriteAllBytes(GetPath((ReportTypeOption)belge.ReportType), belge.TemplateContent);
                    }
                }
                ent.SaveChanges();
            }
        }
    }
}
